package com.niuke.richang;

public class LeetCode35 {

    /**
     * 二分查找法 给定一个排序数组和一个目标值，在数组中找到目标值
     * 并返回索引。
     * @param nums
     * @param target
     * @return
     */
    public static int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int mid = (left + right) >>> 1;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return right;
    }

    public static void main(String[] args) {
        int nums[] = {1,7,9,12,13,15};
        System.out.println(searchInsert(nums, 1));
        System.out.println(searchInsert(nums, 12));
        System.out.println(searchInsert(nums, 14));
        System.out.println("1111");
    }
}
